<html>
<head><meta charset="utf-8"><title>Const generic specialisations for recursive enums · t-compiler/const-eval · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/index.html">t-compiler/const-eval</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Const.20generic.20specialisations.20for.20recursive.20enums.html">Const generic specialisations for recursive enums</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="188345556"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Const%20generic%20specialisations%20for%20recursive%20enums/near/188345556" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Zachary Freed <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Const.20generic.20specialisations.20for.20recursive.20enums.html#188345556">(Feb 17 2020 at 00:33)</a>:</h4>
<p>I was playing around with const generics and I tried making a recursive type that was recursive over a const parameter. This should work, so long as there was a way to specialize for a base case. <br>
As an example, I think this would work for an enum to represent a buddy allocator</p>
<div class="codehilite"><pre><span></span><span class="k">pub</span><span class="w"> </span><span class="k">enum</span> <span class="nc">Block</span><span class="o">&lt;</span><span class="k">const</span><span class="w"> </span><span class="n">Level</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">Allocated</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">Free</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">Split</span><span class="p">(</span><span class="n">Block</span><span class="o">&lt;</span><span class="p">{</span><span class="w"> </span><span class="n">Level</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="p">}</span><span class="o">&gt;</span><span class="p">,</span><span class="w"> </span><span class="n">Block</span><span class="o">&lt;</span><span class="p">{</span><span class="w"> </span><span class="n">Level</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="p">}</span><span class="o">&gt;</span><span class="p">),</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">pub</span><span class="w"> </span><span class="k">enum</span> <span class="nc">Block</span><span class="o">&lt;</span><span class="mi">0</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">Allocated</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">Free</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>


<p>I think this would work as it has a fully known size, and so it shouldn't need a pointer type for the recursion, which is quite useful for embedded stuff.</p>



<a name="188421020"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Const%20generic%20specialisations%20for%20recursive%20enums/near/188421020" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Zachary Freed <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Const.20generic.20specialisations.20for.20recursive.20enums.html#188421020">(Feb 18 2020 at 03:20)</a>:</h4>
<p>I think having GADTs would also allow this?</p>



<a name="188429800"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Const%20generic%20specialisations%20for%20recursive%20enums/near/188429800" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Zachary Freed <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Const.20generic.20specialisations.20for.20recursive.20enums.html#188429800">(Feb 18 2020 at 07:48)</a>:</h4>
<p>I think having GADTs would also allow this?</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>